"""
Case Type   : 资源池化
Case Name   : 资源池化场景下，配置archive_command参数的多种情况
Create At   : 2025-03-11
Owner       : hejiahuan
Description :
    1、查看集群状态
    2、连接数据库进行不同的情况的archive_command配置
    3、恢复集群初始状态
Expect      :
    1、集群状态正常，数据库节点状态正常
    2、不同情况的archive_command配置均符合预期
    3、恢复集群原始状态成功,集群状态正常
History     :
    1、2025-03-11 创建
    2、2025-07-09 修改为1p1s环境执行用例
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()

    def test_shared_storage(self):
        text = '-----step1:查看集群状态; expect:集群状态正常,若不正常则重启集群重新查看-----'
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        if "can't connect to cm_server" in status:
            stopdn = Primary_SH.exec_cm_ctl('stop')
            self.log.info(stopdn)
            status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
            startdn = Primary_SH.exec_cm_ctl('start')
            self.log.info(startdn)
            self.assertIn(self.constant.cm_start_success_msg, startdn,
                    '拉起集群失败')
            status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
            self.log.info(status)
        else:
            self.log.info(status + "集群状态正常")
        self.log.info(status)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status, '执行失败' + text)

        text = '-----step2:连接数据库进行不同情况的archive_command配置; expect:输出情况符合预期-----'
        self.log.info(text)
        archive_command_null = "alter system set archive_command = '';"
        archive_command_status = Primary_SH.execut_db_sql(sql=archive_command_null)
        self.log.info(archive_command_status)
        self.assertIn('Archive_command cannot be empty in runtime.', archive_command_status,
                      '执行失败' + text)
        archive_command_overlength = "alter system set archive_command = 'aaaaaaaaaaaaaaaaaaaaaa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H.execut_db_sql(sql=archive_command_overlength)
        self.log.info(archive_command_status)
        self.assertIn('Archive_command is too long. Please check and make sure it is correct.', archive_command_status,
                      '执行失败' + text)
        archive_command_f = "alter system set archive_command = '%f';"
        archive_command_status = Primary_SH.execut_db_sql(sql=archive_command_f)
        self.log.info(archive_command_status)
        self.assertIn('Archive_command should be set with "%p".', archive_command_status,
                      '执行失败' + text)
        archive_command_p = "alter system set archive_command = '%p';"
        archive_command_status = Primary_SH.execut_db_sql(sql=archive_command_p)
        self.log.info(archive_command_status)
        self.assertIn('Archive_command should be set with "%f".', archive_command_status,
                      '执行失败' + text)
        archive_command_pf = "alter system set archive_command = '%p %f';"
        archive_command_status = Primary_SH.execut_db_sql(sql=archive_command_pf)
        self.log.info(archive_command_status)
        self.assertIn('ALTER SYSTEM SET', archive_command_status,
                      '执行失败' + text)

    def tearDown(self):
        text = '-----step3:恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)

        status = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(status)
        self.assertIn(self.constant.cm_stop_success_msg, status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='start')
        self.log.info(status)
        self.assertIn(self.constant.cm_start_success_msg, status, '执行失败' + text)

        recovery_switchover = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(recovery_switchover)
        self.assertIn(self.constant.cm_switchover_success_msg, recovery_switchover,
                      '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('P Primary Normal',
                      status.splitlines()[-1].split('|')[0].split('6001')[-1],
                      '执行失败' + text)
        self.assertIn('S Standby Normal',
                      status.splitlines()[-1].split('|')[1].split('6002')[-1],
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
